package cn.melonlib.cores.aop.comp.inject;

import cn.hutool.core.bean.BeanUtil;
import cn.melonlib.cores.commons.comp.utils.AdvancedStringUtils;
import cn.melonlib.cores.commons.model.entity.RecordEntity;
import cn.melonlib.cores.identification.comp.messenger.IdentificationMessenger;
import cn.melonlib.cores.identification.model.entity.Identification;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;

@Component
public class InjectRecordInterceptor extends ApplicationObjectSupport implements MethodInterceptor {


    @Resource
    private ObjectMapper jsonMapper;

    @Nullable
    @Override
    public Object invoke(@NotNull MethodInvocation methodInvocation) throws Throwable {
        IdentificationMessenger identificationMessenger = this.getApplicationContext().getBean(IdentificationMessenger.class);
        Arrays.stream(methodInvocation.getArguments()).forEach(arg -> {
            Identification identification = identificationMessenger.getIdentification();
            String id= AdvancedStringUtils.defaultIfBlank(BeanUtil.getProperty(identification,"openid"),
                    ()->BeanUtil.getProperty(identification,"account"),
                    ()->BeanUtil.getProperty(identification,"id"),()->""
            );
            String code = AdvancedStringUtils.defaultIfBlank(
                    BeanUtil.getProperty(identification,"username"),
                    ()->BeanUtil.getProperty(identification,"loginName"),
                    ()->BeanUtil.getProperty(identification,"usercode"),
                    ()->BeanUtil.getProperty(identification,"userCode"),
                    ()->BeanUtil.getProperty(identification,"code"),
                    ()->BeanUtil.getProperty(identification,"account"),()->""
            );
            String name = StringUtils.defaultIfBlank(BeanUtil.getProperty(identification, "name"), code);
            if (arg instanceof RecordEntity) {
//                try {
                ((RecordEntity) arg).setCreatorId(String.valueOf(id));
                ((RecordEntity) arg).setCreatorCode(code);
                ((RecordEntity) arg).setCreatorName(name);
//                    ((RecordEntity) arg).setCreator(jsonMapper.writeValueAsString(identificationMessenger.getIdentification()));
//                } catch (JsonProcessingException e) {
//                    logger.error(e.getMessage(),e);
//                }
            } else if (arg instanceof Collection) {
                ((Collection<?>) arg).stream().forEach(argItem -> {
                    if (argItem instanceof RecordEntity) {
//                        try {
                        ((RecordEntity) argItem).setCreatorId(String.valueOf(id));
                        ((RecordEntity) argItem).setCreatorCode(code);
                        ((RecordEntity) argItem).setCreatorName(name);
//                            ((RecordEntity) argItem).setCreator(jsonMapper.writeValueAsString(identificationMessenger.getIdentification()));
//                        } catch (JsonProcessingException e) {
//                            logger.error(e.getMessage(), e);
//                        }
                    }
                });
            }
        });
        return methodInvocation.proceed();
    }
}
